//
//  WYMediaTitleView.m
//  CirclePush
//
//  Created by admin on 2018/4/26.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMediaTitleView.h"
#import "WYMediaReusableView.h"
#import "WYMdeiaCell.h"
#import "WYAttribute.h"

#import "WYConditionView.h"
#import "WYPriceModel.h"

#import "WYMediaAddressController.h"
#import "WYMediaTypeController.h"
#import "WYMediaPriceController.h"


@interface WYMediaTitleView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,assign)WYHot hot;
@property (nonatomic,strong)NSMutableArray *btnArray;
@property (nonatomic,strong)UIButton *sureButton;
@property (nonatomic,strong)UIButton *cancelButton;

@property (nonatomic,strong)WYMediaAddressController *avc;
@property (nonatomic,strong)WYMediaTypeController *tvc;
@property (nonatomic,strong)WYMediaPriceController *pvc;
@property (nonatomic,strong)UIView *maskView;
@end




@implementation WYMediaTitleView
{
    BOOL _isShow;
    BOOL _isDisplay;
    NSUInteger _tag;
}

-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]init];
        _sureButton.backgroundColor = RGBA(59, 128, 249, 1.0);
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        _sureButton.frame = CGRectMake(kScreenWidth/2, 0, kScreenWidth/2, 50);
        [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:(UIControlEventTouchUpInside)];
        _sureButton.hidden=YES;
    }


    return _sureButton;
}

-(void)sureAction
{
    if (_tag==3) {
        [self.priceRangeCell.leftTextfield resignFirstResponder];
        [self.priceRangeCell.rightTextfield resignFirstResponder];
        //WYPriceModel;
        if (!self.priceRangeCell.leftTextfield.text.length&&!self.priceRangeCell.rightTextfield.text.length) {
        }
        if (self.priceRangeCell.leftTextfield.text.length&&!self.priceRangeCell.rightTextfield.text.length) {
            
            WYPriceModel *priceModel = [[WYPriceModel alloc]init];
            priceModel.name = [NSString stringWithFormat:@"%@以上",self.priceRangeCell.leftTextfield.text];
            priceModel.minPrice = self.priceRangeCell.leftTextfield.text;
            if ([self.delegate respondsToSelector:@selector(inputPriceinConditionborderAndType:WithMaxPrice:WithMinPrice:)]) {
                [self.delegate inputPriceinConditionborderAndType:NO WithMaxPrice:nil WithMinPrice:self.priceRangeCell.leftTextfield.text];
            }
        }
        if (!self.priceRangeCell.leftTextfield.text.length&&self.priceRangeCell.rightTextfield.text.length) {
            
            WYPriceModel *priceModel = [[WYPriceModel alloc]init];
            priceModel.name = [NSString stringWithFormat:@"%@以下",self.priceRangeCell.leftTextfield.text];
            priceModel.maxPrice = self.priceRangeCell.leftTextfield.text;
       [self.delegate inputPriceinConditionborderAndType:NO WithMaxPrice:self.priceRangeCell.rightTextfield.text    WithMinPrice:nil];
        }
        if (self.priceRangeCell.leftTextfield.text.length&&self.priceRangeCell.rightTextfield.text.length) {
            WYPriceModel *priceModel = [[WYPriceModel alloc]init];
            priceModel.name = [NSString stringWithFormat:@"%@~%@万",self.priceRangeCell.leftTextfield.text,self.priceRangeCell.rightTextfield.text];
            priceModel.maxPrice = self.priceRangeCell.leftTextfield.text;
             [self.delegate inputPriceinConditionborderAndType:YES WithMaxPrice:self.priceRangeCell.rightTextfield.text    WithMinPrice:self.priceRangeCell.leftTextfield.text];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(clickSureAction)]) {
        [self.delegate clickSureAction];
    }
}


-(NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [[NSMutableArray alloc]init];
    }
    return _btnArray;
}



-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createSubViews];
//        [self initWithMediaTypeCollectionView];
//        [self initWithCityCollectionview];
//        [self initWithPriceCollectionView];
        [self initCollectionView];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeShow) name:@"changeShow" object:nil];
        
    }
    return self;
}
-(void)changeShow
{
    for (UIButton *btn in self.btnArray) {
        btn.selected = NO;
        btn.imageView.transform = CGAffineTransformMakeRotation(0);
    }
    _isDisplay = NO;
    [self.tvc.view removeFromSuperview];
    [self.avc.view removeFromSuperview];
    [self.pvc.view removeFromSuperview];
}
-(void)initCollectionView
{
    WYMediaAddressController *avc = [[WYMediaAddressController alloc]init];
    self.avc = avc;
   // [self.view addSubview:avc.view];
    //[self addChildViewController:avc];
    
    WYMediaTypeController *tvc = [[WYMediaTypeController alloc]init];
    self.tvc = tvc;
   // [self.view addSubview:tvc.view];
    // [self addChildViewController:tvc];
    
    WYMediaPriceController *pvc = [[WYMediaPriceController alloc]init];
    self.pvc = pvc;
    //[self addChildViewController:pvc];
   // [self.view addSubview:pvc.view];
    
    CGFloat navHeight = 64;
    if (kDevice_Is_iPhoneX) {
        navHeight = 88;
    }
    
    
    avc.view.frame = CGRectMake(0, navHeight+44, kScreenWidth, kScreenHeight-44-navHeight);
    
    tvc.view.frame = CGRectMake(0, navHeight+44, kScreenWidth, kScreenHeight-44-navHeight);
    
    pvc.view.frame = CGRectMake(0, navHeight+44, kScreenWidth, kScreenHeight-44-navHeight);
    
    
    
//     self.tvc.view.hidden= YES;
//     self.avc.view.hidden= YES;
//     self.pvc.view.hidden= YES;
//
    
    
    
    
}
-(void)createSubViews
{
    self.hot = WYHotNormal;
    UIButton *mediaTypeBtn = [[UIButton alloc]init];
    [self.btnArray addObject:mediaTypeBtn];
   // mediaTypeBtn.backgroundColor = [UIColor redColor];
    [self addSubview:mediaTypeBtn];
    UIButton *hotBtn = [[UIButton alloc]init];
    [self.btnArray addObject:hotBtn];
   // hotBtn.backgroundColor = [UIColor yellowColor];
    [self addSubview:hotBtn];
    UIButton *PriceBtn = [[UIButton alloc]init];
    [self.btnArray addObject:PriceBtn];
   // PriceBtn.backgroundColor = [UIColor blueColor];
    [self addSubview:PriceBtn];
    UIButton *areaBtn = [[UIButton alloc]init];
    [self.btnArray addObject:areaBtn];
  //  areaBtn.backgroundColor = [UIColor blackColor];
    [self addSubview:areaBtn];
    [mediaTypeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@[hotBtn,PriceBtn,areaBtn]);
        make.left.top.bottom.mas_equalTo(0);
        make.right.equalTo(hotBtn.mas_left).offset(0);
    }];
    
    [hotBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@[mediaTypeBtn,PriceBtn,areaBtn]);
        make.top.bottom.mas_equalTo(0);
        make.left.equalTo(mediaTypeBtn.mas_right).offset(0);
        make.right.equalTo(PriceBtn.mas_left).offset(0);
        
    }];
    
    [PriceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@[mediaTypeBtn,hotBtn,areaBtn]);
        make.top.bottom.mas_equalTo(0);
        make.left.equalTo(hotBtn.mas_right).offset(0);
        make.right.equalTo(areaBtn.mas_left).offset(0);
    }];
    
    [areaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@[mediaTypeBtn,hotBtn,PriceBtn]);
        make.top.bottom.mas_equalTo(0);
        make.left.equalTo(PriceBtn.mas_right).offset(0);
        make.right.mas_equalTo(0);
        
    }];
    
    [mediaTypeBtn setTitle:@"媒体类型" forState:UIControlStateNormal];
    [mediaTypeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    mediaTypeBtn.tag = 1;
    [mediaTypeBtn addTarget:self action:@selector(clickButonWithTag:) forControlEvents:UIControlEventTouchUpInside];
    mediaTypeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
    mediaTypeBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 70, 0, 0);
    [mediaTypeBtn setImage:[UIImage imageNamed:@"icon_down_arrow"] forState:UIControlStateNormal];
    mediaTypeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [mediaTypeBtn setTitleColor:[UIColor hexStringToColor:@"#353535" alpha:1.0] forState:(UIControlStateNormal)];
    
    
    [hotBtn setTitle:@"热度" forState:UIControlStateNormal];
    [hotBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    hotBtn.tag = 2;
    [hotBtn addTarget:self action:@selector(clickButonWithTag:) forControlEvents:UIControlEventTouchUpInside];
    [hotBtn setImage:[UIImage imageNamed:@"icon_sort_normol"] forState:UIControlStateNormal];
    hotBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    hotBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 65, 0, 0);
    
    hotBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [hotBtn setTitleColor:[UIColor hexStringToColor:@"#353535" alpha:1.0] forState:(UIControlStateNormal)];
    
    
    
    [PriceBtn setTitle:@"刊例价" forState:UIControlStateNormal];
    [PriceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    PriceBtn.tag = 3;
    [PriceBtn addTarget:self action:@selector(clickButonWithTag:) forControlEvents:UIControlEventTouchUpInside];
    PriceBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
    PriceBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 65, 0, 0);
    [PriceBtn setImage:[UIImage imageNamed:@"icon_down_arrow"] forState:UIControlStateNormal];
    
    PriceBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [PriceBtn setTitleColor:[UIColor hexStringToColor:@"#353535" alpha:1.0] forState:(UIControlStateNormal)];
    
    
    
    [areaBtn setTitle:@"区域选择" forState:UIControlStateNormal];
    [areaBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    areaBtn.tag = 4;
    [areaBtn addTarget:self action:@selector(clickButonWithTag:) forControlEvents:UIControlEventTouchUpInside];
    areaBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
    areaBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 65, 0, 0);
    [areaBtn setImage:[UIImage imageNamed:@"icon_down_arrow"] forState:UIControlStateNormal];
    areaBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [areaBtn setTitleColor:[UIColor hexStringToColor:@"#353535" alpha:1.0] forState:(UIControlStateNormal)];
    
    
    
    UIView *lineView = [[UIView alloc]init];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kLineHeight);
    }];
    lineView.backgroundColor = [UIColor hexStringToColor:@"#e6e6e6" alpha:1.0];
    
    
    
    
    
 }
-(void)clickButonWithTag:(UIButton *)button
{
    if (button.tag!=2) {
        
        
        
        
        
//        _isDisplay = !_isDisplay;
        if (!_isDisplay) {
            if (button.tag==1) {
                [self.superview addSubview:self.tvc.view];
                [self.avc.view removeFromSuperview];
                [self.pvc.view removeFromSuperview];
                _isDisplay = YES;
            }
            if (button.tag==3) {
                [self.superview addSubview:self.pvc.view];
                [self.tvc.view removeFromSuperview];
                [self.avc.view removeFromSuperview];
                _isDisplay = YES;
            }
            if (button.tag==4) {
                [self.superview addSubview:self.avc.view];
                [self.pvc.view removeFromSuperview];
                [self.tvc.view removeFromSuperview];
                _isDisplay = YES;
            }
            button.selected = !button.isSelected;
            if (button.selected) {
                button.imageView.transform = CGAffineTransformMakeRotation(M_PI);
            }else{
                button.imageView.transform = CGAffineTransformMakeRotation(0);
            }
        }else{
        for (UIButton *btn in self.btnArray) {
            button.selected = NO;
            btn.imageView.transform = CGAffineTransformMakeRotation(0);
        }
            _isDisplay = NO;
            [self.tvc.view removeFromSuperview];
            [self.avc.view removeFromSuperview];
            [self.pvc.view removeFromSuperview];
            return;
        }
    }
    //            for (UIButton *btn in self.btnArray) {
    //                button.selected = NO;
    //                btn.imageView.transform = CGAffineTransformMakeRotation(0);
    //            }
    
    
    
    if (_isShow) {
        _tag=5555;
       
        
        return;
    }

    
    if (button.tag==2) {
        
        switch (self.hot) {
            case WYHotNormal:{
                [button setImage:[UIImage imageNamed:@"icon_sort_top"] forState:UIControlStateNormal];
                self.hot = WYHotHigh;
                if ([self.delegate respondsToSelector:@selector(clickHotTypeWithType:)]) {
                    [self.delegate clickHotTypeWithType:WYHotHigh];
                }
                
                
            }
                
                break;
            case WYHotHigh:{
                 [button setImage:[UIImage imageNamed:@"icon_sort_bottom"] forState:UIControlStateNormal];
                self.hot = WYHotLow;
                if ([self.delegate respondsToSelector:@selector(clickHotTypeWithType:)]) {
                    [self.delegate clickHotTypeWithType:WYHotLow];
                }
            }
                break;
            case WYHotLow:{
                 [button setImage:[UIImage imageNamed:@"icon_sort_normol"] forState:UIControlStateNormal];
                 self.hot = WYHotNormal;
                if ([self.delegate respondsToSelector:@selector(clickHotTypeWithType:)]) {
                    [self.delegate clickHotTypeWithType:WYHotNormal];
                }
                
            }
                break;
                
            default:
                break;
        }
        return;
    }else{
        
    }
    if (0) {
       // _tag=4444;
      //  [self hideView];
    }else{
        _tag = button.tag;
        if (button.tag==1&&self.headDataArray.count) {
        }else{
            if (button.tag==3&&self.priceArray.count>1) {
            }else{
                if (button.tag==4&&self.cityArray.count>1) {
                }else{
                    if (self.complete) {
                        self.complete(button.tag);
                    }
                }
            }
    }
}
}
@end
